package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.QuestionCollection;
import cn.wolfcode.trip.base.domain.QuestionFollow;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.QuestionReplyMapper;
import cn.wolfcode.trip.base.mapper.QuestionFollowMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IQuestionFollowService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class QuestionFollowServiceImpl implements IQuestionFollowService {
    @Autowired
    private QuestionFollowMapper questionFollowMapper;

    @Override
    public void saveOrUpdate(QuestionFollow questionFollow) {
        if (questionFollow.getId() != null) {
            questionFollowMapper.updateByPrimaryKey(questionFollow);
        } else {
            User currentUser = UserContext.getCurrentUser();
            if (currentUser != null) {
                //设置关注人
                questionFollow.setFollowUser(currentUser);
                //关注保存操作
                questionFollowMapper.insert(questionFollow);
            }
        }
    }


    @Override
    public QuestionFollow getById(Long id) {
        return questionFollowMapper.selectByPrimaryKey(id);
    }


    @Override
    public void deleteQuestionFollowById(Long questionId) {
        //获取当前用户的id
        Long userId = UserContext.getCurrentUser().getId();
        //删除当前用户关注的问题
        questionFollowMapper.deleteQuestionFollowById(questionId, userId);
    }

    @Override
    public QuestionFollow queryFollowById(Long questionId) {
        Long userId = UserContext.getCurrentUser().getId();
        QuestionFollow questionFollow = questionFollowMapper.selectFollowById(questionId, userId);
        if (questionFollow == null) {  //查询当前用户是否有关注此问题
            throw new RuntimeException("这货没有关注这个问题啊!");
        }
        return questionFollow;
    }


}
